<template>
	<view class="wrongExercise">
		<view style="height: 25px; width: 750rpx;  background-color:transparent;"></view>
		<my-title @back="back" :title="'章节错题练习'" :iSshow="true" :handles="'../../static/icon/rabbish.png'"
			@clickEvent="open"></my-title>
		<view class="content_list">
			<list :lists="lists" @chapter-select="chapterSelect"></list>
		</view>
		<uni-popup ref="popup" type="dialog">
			<uni-popup-dialog title="" content="确定要清空吗?" :before-close="true" @close="close" @confirm="confirm">
			</uni-popup-dialog>
		</uni-popup>
		<!-- 		<uni-popup ref="popup" :mask-click="false" type="top">
			<view style="margin-top: 50%;" class="popstyle">
				<text>确定要清空么</text>
				<view class="headstyle">
					<view style="width: 50%;position: absolute;text-align: center; color: white; border-radius: 10px 0px 0px 0px ; "  @click="changeform(1)" :class="buttonIndex == 1?'bgcolor':'bgcolor2'">
						<text>顺序练习</text>
					</view>
					<view style="width: 50%;position: absolute;right: 0px;text-align: center;  color: white; border-radius: 0px 10px 0px 0px" @click="changeform1(2)" :class="buttonIndex == 2?'bgcolor':'bgcolor2'">
						<text>随机练习</text>
					</view>
				</view>
			</view>
							
		</uni-popup> -->
	</view>
</template>

<script>
	import list from '../../components/courseList.vue'
	import myTitle from '../../components/title.vue'
	export default {
		components: {
			list,
			myTitle
		},
		data() {
			return {
				lists: [],
				id: '',
				cid: ''
			}
		},
		methods: {
			back() {
				uni.navigateTo({
					url: '/pages/index/index'
				})
			},
			open() {
				this.$refs.popup.open()
			},
			/**
			 * 点击取消按钮触发
			 * @param {Object} done
			 */
			close() {
				// TODO 做一些其他的事情，before-close 为true的情况下，手动执行 close 才会关闭对话框
				// ...
				this.$refs.popup.close()
			},
			/**
			 * 点击确认按钮触发
			 * @param {Object} done
			 * @param {Object} value
			 */
			confirm(value) {
				// 输入框的值
				console.log(value)
				// TODO 做一些其他的事情，手动执行 close 才会关闭对话框
				// ...
				this.$refs.popup.close()
			},
			chapterSelect(cid) {
				this.cid = cid
				console.log('555555555555')
				this.getWrongQuestion()

			},
			getWrongQuestion() {
				console.log(this.cid)
				uni.request({
					url: 'http://exam.tutestudio.net/api/student/wrongQuestion?cid=' + this.cid,
					method: 'get',
					header: {
						token: this.token
					},
					success: (res) => {
						console.log(res)
						if (res.data.statusCode !== 200) {
							uni.showToast({
								title: res.data.errors,
								icon: 'none'
							})
							return;
						}
						if (res.data.data.length === 0) {
							uni.showToast({
								title: '错题内容为空',
								icon: 'none'
							})
							return;
						}
						console.log(res.data.data)
						uni.setStorageSync('exercises', JSON.stringify(res.data.data))
						uni.navigateTo({
							url: '/pages/wrongExercises/wrongQuestion?cid=' + this.id
						})
					},
					fail(err) {
						uni.showToast({
							title: '接口参数有误！',
							icon: 'none'
						})
					}
				})
			},
			getChapter() {
				uni.request({
					url: 'http://exam.tutestudio.net/api/student/chapterWrong?cid=' + this.id,
					method: 'GET',
					header: {
						token: this.token
					},
					success: (res) => {
						if (res.data.statusCode !== 200) {
							uni.showToast({
								title: res.data.errors,
								icon: 'none'
							})
						}
						this.lists = res.data.data
						console.log(this.lists)
					},
					fail(err) {
						uni.showToast({
							title: '接口参数有误！',
							icon: 'none'
						})
					}
				})
			}
		},

		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			this.id = option.id;
			console.log(option)
			this.token = uni.getStorageSync('token')
			if (!this.token) {
				uni.navigateTo({
					url: '../loginpage/loginpage'
				})
				return;
			}
			this.getChapter()
		},
	}
</script>

<style lang="scss" scoped>
	.content_list {
		margin: 20px 10px;
	}
</style>
